package com.sec.android.app.myfiles.decorator;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;

import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

/**
 * Created by jaebae on 2016-01-21.
 */
public abstract class AbsAdapterImpDecorator {
    protected Context mContext;
    protected AbsFileListAdapterImp mAdapterImp;
    protected AbsAdapterImpDecorator mNext;

    protected AbsAdapterImpDecorator(Context context, FileListAdapter adapter) {
        mContext = context;
        mAdapterImp = adapter.getAdapterImp();
    }

    public void bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        _bindView(context, vh, v, record, position);
        if (mNext != null) {
            mNext.bindView(context, vh, v, record, position);
        }
    }

    public final void createViewHolder(View v, ListViewHolder vh) {
        _createViewHolder(v, vh);

        if (mNext != null) {
            mNext.createViewHolder(v, vh);
        }
    }

    public final void add(AbsAdapterImpDecorator next) {
        if (mNext == null) {
            mNext = next;
        } else {
            mNext.add(next);
        }
    }

    public final void bindCreateFolderView(ListViewHolder vh, View v) {
        _bindCreateFolderView(vh, v);
        if (mNext != null) {
            mNext.bindCreateFolderView(vh, v);
        }
    }

    public final void bindProgressSync(ListViewHolder vh, View v, FileRecord record, boolean isShow) {
        _bindProgressSync(vh, v, record, isShow);
        if (mNext != null) {
            mNext._bindProgressSync(vh, v,record, isShow);
        }
    }

    public final void getFileRecord(Cursor cursor, FileRecord record) {
        if (!_getFileRecord(cursor, record)) {
            updateRecordDetail(record);
        }
        if (mNext != null) {
            mNext.getFileRecord(cursor, record);
        }
    }

    public final View getView(int position, View convertView, ViewGroup parent) {
        View ret = _getView(position, convertView, parent);
        if (ret == null && mNext != null) {
            ret = mNext.getView(position, convertView, parent);
        }

        return ret;
    }

    protected View _getView(int position, View convertView, ViewGroup parent) {
        return null;
    }

    public final void scrapView(int position, View view) {
        _scrapView(position, view);
        if (mNext != null) {
            mNext.scrapView(position, view);
        }
    }

    protected void _scrapView(int position, View view) {
    }


    protected boolean _getFileRecord(Cursor cursor, FileRecord record) {
        return true;
    }

    protected void updateRecordDetail(FileRecord record) {
    }


    protected abstract void _bindCreateFolderView(ListViewHolder vh, View v);

    protected abstract void _createViewHolder(View v, ListViewHolder vh);

    protected abstract void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position);

    protected abstract void _bindProgressSync(ListViewHolder vh, View v, FileRecord record, boolean isShow);
}
